
/*
剑指 Offer 04. 二维数组中的查找
解题思路：线性查找，先从最后一行第一个开始找，当target等于当前数，则返回true，当target小于当前数，则往上一行，当target大于当前数，则向右一位
 */
public class Offer04 {

  public static boolean findNumberIn2DArray(int[][] matrix, int target) {
    if (matrix == null || matrix.length ==0){
      return false;
    }
    int rows = matrix.length;
    int colmns = matrix[0].length;
    int row = 0;
    int colmn = colmns - 1;
    while (row <rows && colmn >= 0){
      int num = matrix[row][colmn];
      if (num == target){
        return true;
      }else if (num > target){
        colmn--;
      }else {
        row++;
      }
    }
    return false;
  }

  public static void main(String[] args) {
    int[][] num = new int[][]
            {{1,4,7,11,1},{2,5,8,12,1},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}
  };
    System.out.println(Offer04.findNumberIn2DArray(num, 6));

  }
}
